<!DOCTYPE html>
<html lang="zxx">

<head>
    <title>小组管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8">
    <meta name="keywords" content="小组管理">
    <link href="//fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@400;500;700;900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css" type="text/css" media="all">
    <link rel="stylesheet" href="css/font-awesome.min.css" type="text/css" media="all">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>

<body>
<section class="w3l-hotair-form">
    <h1>小组管理页面</h1>
    <div class="container">
        <div class="workinghny-form-grid">
            <div class="main-hotair">
                <div class="content-wthree">
                    <h2>增加小组</h2>
                    <form id="addGroupForm">
                        <input type="text" class="text" name="groupName" placeholder="小组名称" required>
                        <button class="btn" type="submit">增加</button>
                    </form>

                    <h2>删除小组</h2>
                    <form id="deleteGroupForm">
                        <input type="number" class="text" name="groupId" placeholder="小组ID" required>
                        <button class="btn" type="submit">删除</button>
                    </form>

                    <h2>修改小组</h2>
                    <form id="updateGroupForm">
                        <input type="number" class="text" name="groupId1" placeholder="小组ID" required>
                        <input type="text" class="text" name="newGroupName" placeholder="新小组名称" required>
                        <button class="btn" type="submit">修改</button>
                    </form>

                    <h2>查询小组</h2>
                    <form id="getGroupForm">
                        <input type="number" class="text" name="groupId2" placeholder="小组ID" required>
                        <button class="btn" type="submit">查询</button>
                    </form>

                    <div id="groupResult"></div>

                    <!-- 新增返回主页按钮 -->
                    <button id="backToHomeBtn" class="btn">返回主页</button>
                </div>
                <div class="w3l_form align-self">
                    <div class="left_grid_info">
                        <img src="images/1.png" alt="管理图片" class="img-fluid">
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<script>
    $(document).ready(function() {
        // 增加小组
        $('#addGroupForm').on('submit', function(event) {
            event.preventDefault();

            var groupName = $('input[name="groupName"]').val();

            $.ajax({
                url: 'http://localhost:8080/Servlet/addGroup',
                method: 'POST',
                data: {
                    name: groupName
                },
                success: function(response) {
                    alert(response.message);
                },
                error: function(xhr, status, error) {
                    alert('请求失败，请重试');
                }
            });
        });

        // 删除小组
        $('#deleteGroupForm').on('submit', function(event) {
            event.preventDefault();

            var groupId = $('input[name="groupId"]').val();

            $.ajax({
                url: 'http://localhost:8080/Servlet/deleteGroup',
                method: 'POST',
                data: {
                    id: groupId
                },
                dataType: 'json', // 确保服务器返回JSON格式的数据
                success: function(response) {
                    alert(response.message);
                },
                error: function(xhr, status, error) {
                    alert('请求失败，请重试');
                }
            });
        });

        // 修改小组
        $('#updateGroupForm').on('submit', function(event) {
            event.preventDefault();

            var groupId = $('input[name="groupId1"]').val();
            var newGroupName = $('input[name="newGroupName"]').val();

            console.log("Group ID: " + groupId);
            console.log("New Group Name: " + newGroupName);

            $.ajax({
                url: 'http://localhost:8080/Servlet/updateGroup', // 确保URL路径正确
                method: 'POST',
                data: {
                    id: groupId,
                    name: newGroupName
                },
                dataType: 'json', // 确保服务器返回JSON格式的数据
                success: function(response) {
                    alert(response.message);
                },
                error: function(xhr, status, error) {
                    alert('请求失败，请重试');
                }
            });
        });

        // 查询小组
        $('#getGroupForm').on('submit', function(event) {
            event.preventDefault();

            var groupId = $('input[name="groupId2"]').val();

            $.ajax({
                url: 'http://localhost:8080/Servlet/getGroup',
                method: 'GET',
                data: {
                    id: groupId
                },
                dataType: 'json', // 确保jQuery自动将响应解析为JSON
                success: function(response) {
                    var group = response.group;
                    var members = response.members;
                    var result = '<h3>小组信息</h3>';
                    result += '<p>小组ID: ' + group.id + '</p>';
                    result += '<p>小组名称: ' + group.name + '</p>';
                    result += '<h4>成员列表</h4>';
                    result += '<ul>';
                    members.forEach(function(member) {
                        result += '<li>成员ID: ' + member.id + ', 成员名称: ' + member.name + '</li>';
                    });
                    result += '</ul>';
                    $('#groupResult').html(result);
                },
                error: function(xhr, status, error) {
                    alert('请求失败，请重试');
                }
            });
        });

        // 返回主页
        $('#backToHomeBtn').on('click', function() {
            window.location.href = 'http://localhost:8080/Servlet/Log_HTML/home.html'; // 跳转到主页
        });
    });
</script>
</body>

</html>
